package com.custom.spring.start.mvc;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.embedded.FilterRegistrationBean;
import org.springframework.boot.context.embedded.ServletRegistrationBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.DispatcherServlet;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * ClassName: EnableQuickMvcConfig
 * Description:
 *
 * @author kang.wang03
 *         Date 16/5/26
 */
@Configuration
@EnableConfigurationProperties(MvcConfig.class)
public class EnableQuickMvcConfig {
    @Autowired(required = false)
    private MvcConfig mvcConfig;

    @ConditionalOnMissingBean
    public MvcConfig mvcConfig() {
        return new MvcConfig();
    }
    @Bean(name = EnableQuickMvcConfig.DispatcherServletName)
    @ConditionalOnMissingBean
    public ServletRegistrationBean dispatcherServlet(
    ) {
        DispatcherServlet dispatcherServlet = new DispatcherServlet();
        ServletRegistrationBean filterRegistrationBean = new ServletRegistrationBean();
        filterRegistrationBean.setServlet(dispatcherServlet);
        Map<String, String> params = new HashMap<String, String>();
        params.put("contextConfigLocation",mvcConfig.getContextConfigLocation()==null?
                "classpath:spring-mvc.xml": mvcConfig.getContextConfigLocation());
        filterRegistrationBean.setInitParameters(params);
        filterRegistrationBean.setLoadOnStartup(mvcConfig.getLoadOnStartup());
        if (mvcConfig.getUrlMappings()==null||mvcConfig.getUrlMappings().isEmpty()){
            mvcConfig.setUrlMappings(new ArrayList<String>());
            mvcConfig.getUrlMappings().add("/");
        }
        String strs[] = new String[mvcConfig.getUrlMappings().size()];
        for (int i = 0; i < mvcConfig.getUrlMappings().size(); i++) {
            strs[i] = mvcConfig.getUrlMappings().get(i);
        }
        filterRegistrationBean.addUrlMappings(strs);
        return filterRegistrationBean;
    }
    /**
     * 用户filter配置
     *
     * @return
     */
    @Bean(name = EnableQuickMvcConfig.CharacterEncodingFilterName)
    @ConditionalOnMissingBean
    public FilterRegistrationBean filterRegistrationBean(
    ) {
        CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
        characterEncodingFilter.setEncoding(mvcConfig.getEncoding());
        characterEncodingFilter.setForceEncoding(true);
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setFilter(characterEncodingFilter);
        filterRegistrationBean.addUrlPatterns("/*");
        return filterRegistrationBean;
    }

    public static final String CharacterEncodingFilterName="org.springframework.web.filter.CharacterEncodingFilter";
    public static final String DispatcherServletName=" org.springframework.web.servlet.DispatcherServlet";
}
